import { CloudBoostService } from '../service/cloud-boost.service';
import { ReflectiveInjector } from '@angular/core';
import { Validator, Schema } from 'jsonschema';
import { CloudObject, CloudQuery } from 'cloudboost';

export class Article {

    public title: string;
    public content: string;
    public prevImg?: string;
    public prevText?: string;
    public addtime?: string;
    public keywords?: string[];
    public viewCount?: number;
    public id?: string | number;
    private $cb: CloudBoostService;
    // public articleSchema = SchemaService.creat({})
    private articleSetSchema = {
        id: '/articleSetSchema',
        type: 'object',
        properties: {
            title: { type: 'string' },
            content: { type: 'string' },
            prevImg: { type: 'string' },
            prevText: { type: 'string' },
            addtime: { type: 'string' },
            keywords: { type: 'array' },
            viewCount: { type: 'number' },
            id: { type: 'string,number' }
        }
    }
    private articleStantardSchema = JSON.parse(JSON.stringify(this.articleSetSchema));
    public $v: Validator = new Validator();
    private tableName = 'Article';//数据库表名
    constructor(title: string = '', content: string = '') {
        this.$cb = ReflectiveInjector.resolveAndCreate([CloudBoostService]).get(CloudBoostService);
        this.articleStantardSchema.id = 'articleStantardSchema';
        this.articleStantardSchema.required = ['title', 'content']
        this.$v.addSchema(this.articleSetSchema, '/articleSetSchema')
        this.$v.addSchema(this.articleStantardSchema, '/articleStantardSchema')

        //this.cloudObject =new this.$cb.cb.CloudObject('Article');

    }
    setProperties(opt: any) {
        let validatorResult = this.$v.validate(opt, this.articleSetSchema).errors
        if (!validatorResult.length) {
            for (let item in opt) {
                this[item] = opt[item]
            }
        } else {
            console.error(validatorResult)
        }
        return this;
    }
    getProperties(props: string | string[]) {
        if (typeof props === 'string') {
            return this[props]
        } else if (props instanceof Array) {
            let res = {}
            for (let item of props) {
                res[item] = this[item] || null
            }
            return res
        } else {
            console.warn('unnkown params.it exect a param instans of string or array')
            return null
        }
    }
    add(article: {}) {
        let cloudObject = new CloudObject(this.tableName)
        let validatorResult = this.$v.validate(article, this.articleStantardSchema).errors
        if (!validatorResult.length) {
            for (let key in article) {
                cloudObject.set(key, article[key])
            }
          return  cloudObject.save()//.then(res => { console.log(res, "object saved successfully"); }).error(err => console.log(err, "error while saving object"))
        } else {
           // console.error(validatorResult)
            return new Promise((resolve,reject)=>{
                reject(validatorResult)
            })
        }
    }
    del(article: CloudObject) {
        return article.delete()//.then(res=>console.log(res,'delete article success')).error(err=>console.log(err,'delete failed'))
    }
    getById(id: string) {

    }




}